package formServlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class GetStateListServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{


		System.out.println("hhhhhheeeeerrrrreeee");
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        Query q = new Query("PlaceInfo");
		String country= request.getParameter("country");
		System.out.println(country);
		q.addFilter("Country", Query.FilterOperator.EQUAL,country  );
		PreparedQuery pq = datastore.prepare(q);
		int count=pq.countEntities(FetchOptions.Builder.withPrefetchSize(1));
		System.out.println("Count is " + count);
		String s=new String();
        if(count>0)
		{
			for (Entity result : pq.asIterable())
			{
				System.out.println(result.getKind());
				s=s+(String) result.getProperty("State");
				
		    }
	    }
        else
        {
        	s=s+"hi";
        }

        System.out.println("s is: " + s);
		response.setContentType("text/html");
		response.getWriter().write(s);
		
//		System.out.print("end of statelist");
			
	}
}
